<?php

namespace We7\Dev\DebugBar;

use DebugBar\DataCollector\ExceptionsCollector;
use DebugBar\DataCollector\MemoryCollector;
use DebugBar\DataCollector\MessagesCollector;
use DebugBar\DataCollector\PhpInfoCollector;
use DebugBar\DataCollector\TimeDataCollector;
use DebugBar\DataCollector\PDO\PDOCollector;
use DebugBar\Storage\FileStorage;

class We7DebugBar {
	private $debugBar;

	public function __construct($storage_path) {
		$this->debugBar = new \DebugBar\DebugBar();
		$this->addCollector(new PhpInfoCollector());
		$this->addCollector(new MessagesCollector());
		$this->addCollector(new RequestDataCollector());
		$this->addCollector(new TimeDataCollector());
		$this->addCollector(new MemoryCollector());
		$this->addCollector(new ExceptionsCollector());
		//        dd(pdo()->getPDO());
		$this->addCollector(new PDOCollector(\pdo()->getPDO()));
		$this->addCollector(new CacheDataCollector());
		$this->addCollector(new TemplateDataCollector());
		$this->debugBar->setStorage(new FileStorage($storage_path));
	}

	private function addCollector($val) {
		$this->debugBar->addCollector($val);
	}

	public function showOnShutdown() {
		$renderer = $this->debugBar->getJavascriptRenderer();
		$renderer->renderOnShutdownWithHead();
	}

	public function setOpenHandlerUrl($url) {
		$this->debugBar->getJavascriptRenderer()->setOpenHandlerUrl($url);
	}

	private static function injectHtml($html, $code, $before) {
		$pos = strripos($html, $before);
		if ($pos === false) {
			return $html.$code;
		}

		return substr($html, 0, $pos).$code.substr($html, $pos);
	}
}
